package cn.cheegon.blog.service.impl;

import cn.cheegon.blog.common.api.CommonPage;
import cn.cheegon.blog.common.exception.Asserts;
import cn.cheegon.blog.entity.dto.MessageDto;
import cn.cheegon.blog.entity.rsp.MessageListRsp;
import cn.cheegon.blog.mapper.UmsAdminMapper;
import cn.cheegon.blog.model.CmsMessage;
import cn.cheegon.blog.mapper.CmsMessageMapper;
import cn.cheegon.blog.model.UmsAdmin;
import cn.cheegon.blog.service.CmsMessageService;
import cn.cheegon.blog.service.UmsAdminService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 消息表 服务实现类
 * </p>
 *
 * @author cheegon
 * @since 2024-06-21
 */
@Service
public class CmsMessageServiceImpl extends ServiceImpl<CmsMessageMapper, CmsMessage> implements CmsMessageService {
    @Autowired
    UmsAdminMapper umsAdminMapper;

    @Autowired
    UmsAdminService umsAdminService;

   // @Value("${images.nginxPath}")
    String imgUrl = "";

    @Override
    public CommonPage<MessageDto> getList(int pageNum, int pageSize) {

        UmsAdmin currUser = umsAdminService.getCurrUser();

        QueryWrapper<CmsMessage> queryWrapper = new QueryWrapper<>();
        if (currUser != null) {
            queryWrapper.and(wq -> wq.eq("status", 1)
                    .or()
                    .eq("uid", currUser.getId()));
        } else {
            queryWrapper.eq("status", 1);
        }
        queryWrapper.orderByDesc("create_time");
        Page<CmsMessage> cmsMessagePage = this.baseMapper.selectPage(new Page<>(pageNum, pageSize), queryWrapper);
        if (cmsMessagePage.getRecords().isEmpty()) {
            return CommonPage.restPage(cmsMessagePage);
        }
        List<Long> uidList = cmsMessagePage
                .getRecords()
                .stream()
                .filter(it -> it.getAnonymous() == 0)
                .map(CmsMessage::getUid)
                .distinct()
                .toList();
        if (CollectionUtils.isEmpty(uidList)) {
            return CommonPage.restPage(cmsMessagePage, MessageDto.class);
        }

        List<UmsAdmin> umsAdmins = umsAdminMapper.selectBatchIds(uidList);
        if (CollectionUtils.isEmpty(umsAdmins)) {
            return CommonPage.restPage(cmsMessagePage, MessageDto.class);
        }
        Map<Long, UmsAdmin> adminMap = umsAdmins.stream().collect(Collectors.toMap(UmsAdmin::getId, it -> it));
        List<MessageDto> dtoList = cmsMessagePage.getRecords().stream().map(it -> {
            MessageDto messageDto = new MessageDto();
            BeanUtils.copyProperties(it, messageDto);
            UmsAdmin umsAdmin = adminMap.get(it.getUid());
            if (currUser != null && Objects.equals(it.getUid(), currUser.getId())) {
                messageDto.setSelf(1);
            }
            if (umsAdmin != null&&it.getAnonymous() == 0) {
                messageDto.setNickname(umsAdmin.getNickname());
                messageDto.setAvatar(umsAdmin.getAvatar());
            } else {
                messageDto.setNickname("火星网友");
                messageDto.setAvatar(imgUrl + "mars_dog.jpg");
            }
            return messageDto;
        }).toList();
        return CommonPage.restPage(cmsMessagePage, dtoList);
    }
    @Override
    public CommonPage<MessageListRsp> getAdminList(Integer status, int pageNum, int pageSize) {

        UmsAdmin currUser = umsAdminService.getCurrUser();

        QueryWrapper<CmsMessage> queryWrapper = new QueryWrapper<>();
        if (status != null) {
            queryWrapper.eq("status", status);
        }
        queryWrapper.orderByDesc("create_time");
        Page<CmsMessage> cmsMessagePage = this.baseMapper.selectPage(new Page<>(pageNum, pageSize), queryWrapper);
        if (cmsMessagePage.getRecords().isEmpty()) {
            return CommonPage.restPage(cmsMessagePage);
        }
        List<Long> uidList = cmsMessagePage
                .getRecords()
                .stream()
//                .filter(it -> it.getAnonymous() == 0)
                .map(CmsMessage::getUid)
                .distinct()
                .toList();
        if (CollectionUtils.isEmpty(uidList)) {
            return CommonPage.restPage(cmsMessagePage, MessageListRsp.class);
        }

        List<UmsAdmin> umsAdmins = umsAdminMapper.selectBatchIds(uidList);
        if (CollectionUtils.isEmpty(umsAdmins)) {
            return CommonPage.restPage(cmsMessagePage, MessageListRsp.class);
        }
        Map<Long, UmsAdmin> adminMap = umsAdmins.stream().collect(Collectors.toMap(UmsAdmin::getId, it -> it));
        List<MessageListRsp> dtoList = cmsMessagePage.getRecords().stream().map(it -> {
            MessageListRsp messageDto = new MessageListRsp();
            BeanUtils.copyProperties(it, messageDto);
            UmsAdmin umsAdmin = adminMap.get(it.getUid());
            if (currUser != null && Objects.equals(it.getUid(), currUser.getId())) {
                messageDto.setSelf(1);
            }
            if (umsAdmin != null) {
                messageDto.setNickname(umsAdmin.getNickname());
                messageDto.setAvatar(umsAdmin.getAvatar());
                messageDto.setUid(umsAdmin.getId());
            } else {
                messageDto.setNickname("火星网友");
                messageDto.setAvatar(imgUrl + "mars_dog.jpg");
            }
            return messageDto;
        }).toList();
        return CommonPage.restPage(cmsMessagePage, dtoList);
    }

    @Override
    public void add(CmsMessage dto) {
        UmsAdmin currUser = umsAdminService.getCurrUser();
        dto.setUid(currUser.getId());
        baseMapper.insert(dto);
    }

    @Override
    public void edit(CmsMessage dto) {
        CmsMessage cmsMessage = baseMapper.selectById(dto.getId());
        if (cmsMessage != null) {
            baseMapper.updateById(dto);
        } else {
            Asserts.fail("无此项");
        }
    }
    @Override
    public void editSelf(CmsMessage dto) {
        UmsAdmin currUser = umsAdminService.getCurrUser();
        CmsMessage cmsMessage = baseMapper.selectById(dto.getId());
        if (cmsMessage != null && cmsMessage.getUid().equals(currUser.getId())) {
            dto.setStatus(0);
            baseMapper.updateById(dto);
        } else {
            Asserts.fail("无权限");
        }
    }

    @Override
    public void del(Long id) {
        CmsMessage cmsMessage = baseMapper.selectById(id);
        if (cmsMessage != null) {
            baseMapper.deleteById(id);
        } else {
            Asserts.fail("无此项");
        }
    }
    @Override
    public void delSelf(Long id) {
        UmsAdmin currUser = umsAdminService.getCurrUser();
        CmsMessage cmsMessage = baseMapper.selectById(id);
        if (cmsMessage != null && cmsMessage.getUid().equals(currUser.getId())) {
            baseMapper.deleteById(id);
        } else {
            Asserts.fail("无权限");
        }
    }
}
